package com.nbsaas.boot.app.ext.resource;

import com.nbsaas.boot.app.api.apis.AppRoleApi;
import com.nbsaas.boot.app.api.apis.AppUserRoleApi;
import com.nbsaas.boot.app.api.domain.field.AppUserRoleField;
import com.nbsaas.boot.app.api.domain.request.AppUserRoleRequest;
import com.nbsaas.boot.app.api.domain.response.AppRoleResponse;
import com.nbsaas.boot.app.api.domain.response.AppUserRoleResponse;
import com.nbsaas.boot.app.ext.apis.AppUserRoleExtApi;
import com.nbsaas.boot.app.ext.domain.request.UpdateRoleRequest;
import com.nbsaas.boot.rest.filter.Filter;
import com.nbsaas.boot.rest.response.ResponseObject;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



@Service
public class AppUserRoleExtResource implements AppUserRoleExtApi {

    @Resource
    private AppUserRoleApi appUserRoleApi;

    @Resource
    private AppRoleApi appRoleApi;

    @Transactional
    @Override
    public ResponseObject<AppUserRoleResponse> create(AppUserRoleRequest request) {
        if (request.getRole() == null) {
            return ResponseObject.error("角色id不能为空");
        }
        AppRoleResponse role = appRoleApi.oneData(Filter.eq("id", request.getRole()));
        if (role == null) {
            return ResponseObject.error("角色不存在");
        }
        request.setApp(role.getApp());
        return appUserRoleApi.create(request);
    }

    @Transactional
    @Override
    public ResponseObject<?> updateRole(UpdateRoleRequest request) {
        ResponseObject<?> result = new ResponseObject<>();
        if (request.getUser() == null) {
            return ResponseObject.error("用户id不能为空");
        }

        if (request.getApp() == null) {
            appUserRoleApi.deleteByFilter(
                    Filter.eq(AppUserRoleField.userId, request.getUser())
            );
        } else {
            appUserRoleApi.deleteByFilter(
                    Filter.eq(AppUserRoleField.app, request.getApp()),
                    Filter.eq(AppUserRoleField.userId, request.getUser())
            );
        }


        updateRoles(request);


        return result;
    }

    @Transactional
    @Override
    public ResponseObject<?> updateRoleAll(UpdateRoleRequest request) {
        ResponseObject<?> result = new ResponseObject<>();
        if (request.getUser() == null) {
            return ResponseObject.error("用户id不能为空");
        }
        appUserRoleApi.deleteByFilter(
                Filter.eq(AppUserRoleField.userId, request.getUser())
        );

        updateRoles(request);


        return result;
    }

    private void updateRoles(UpdateRoleRequest request) {
        if (request.getRoles() != null) {
            for (Long roleId : request.getRoles()) {
                AppRoleResponse role = appRoleApi.oneData(Filter.eq("id", roleId));
                if (role == null) {
                    continue;
                }
                AppUserRoleRequest roleRequest = new AppUserRoleRequest();
                roleRequest.setRole(roleId);
                roleRequest.setUserId(request.getUser());
                roleRequest.setApp(role.getApp());
                roleRequest.setTenantId(role.getTenantId());
                appUserRoleApi.create(roleRequest);
            }
        }
    }
}
